background_image_filename = 'allcolors.png'
import pygame
from sys import exit
from pygame.locals import *
import time

def panduan(x, y):
    w, z, c, v, h, g, s, d= 0, 0, 0, 0, 0, 0, 0, 0
    def left_up(x, y):
        nonlocal w
        if w < 5 and (x-58,y-48) in li:
            w += 1 
            left_up(x-58, y-48)
        return w
    def right_down(x, y):
        nonlocal z
        if z < 5 and (x+58,y+48) in li:
            z += 1
            right_down(x+58,y+48)
        return z
    def right_up(x, y):
        nonlocal c
        if c < 5 and (x+58,y-48) in li:
                c += 1 
                right_up(x+58, y-48)
        return c
    def left_down(x, y):
        nonlocal v
        if v < 5 and (x-58,y+48) in li:
                v += 1
                left_down(x-58,y+48)
        return v
    def le_ri(x, y):
        nonlocal s
        if s < 5 and (x-58,y) in li:
                s += 1
                le_ri(x-58,y)
        return s
    def ri_le(x, y):
        nonlocal d
        if d < 5 and (x+58,y) in li:
                d += 1
                ri_le(x+58,y)
        return d
    def up_down(x, y):
        nonlocal g
        if g < 5 and (x,y+48) in li:
                g += 1
                up_down(x,y+48)
        return g
    def down_up(x, y):
        nonlocal h
        if h < 5 and (x,y-48) in li:
                h += 1
                down_up(x,y-48)
        return h
    return [down_up(x, y)+up_down(x, y),ri_le(x, y)+le_ri(x, y),left_down(x, y)+right_up(x, y),left_up(x, y)+right_down(x, y)]

pos_list_white,pos_list_black = [], []
pos_li = []
li = pos_li

pygame.init()
SCREEN_SIZE = (1280, 720)
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("小马哥五子棋")

font = pygame.font.Font("fonts/simsun.ttc", 20)
text_surface = font.render('轮到白棋出棋', True, (255, 255, 255))
text_surface_turn = font.render('轮到黑棋出棋',True,(255, 255, 255))
text_surface_bsuccess = font.render('黑棋胜利!',True,(255, 255, 255))
text_surface_wsuccess = font.render('白棋胜利!',True,(255, 255, 255))
#绘制地图
background = pygame.image.load(background_image_filename).convert()
Flag, Flag_w, Flagger, Turn = False, False, True, 0
while True:
    screen.blit(background, (0, 0))
    screen.set_clip(0, 0, SCREEN_SIZE[0], SCREEN_SIZE[1]/15)
    screen.fill((170,150,0))
    if Turn:screen.blit(text_surface, (0, 0))
    if not Turn:screen.blit(text_surface_turn, (1000, 0))
    if Flag_w:
        screen.blit(text_surface_wsuccess, (500, 0))
        pygame.display.update()
        Flagger = False
    if Flag:
        screen.blit(text_surface_bsuccess, (500, 0))
        pygame.display.update()
        Flagger = False
    screen.set_clip(0, SCREEN_SIZE[1]/15, SCREEN_SIZE[0], SCREEN_SIZE[1])
    x,y = pygame.mouse.get_pos()
    x = (x-(x%58)) if x%58 <= 29 else (x-(x%58)) +58
    y = (y-(y%48)) if y%48 <= 24 else (y-(y%48)) + 48
    for event in pygame.event.get():
        if event.type == QUIT:exit()
    if event.type == MOUSEBUTTONDOWN and not Turn and Flagger:
        if (x,y) not in pos_li:
            pygame.draw.circle(background, (0, 0, 0), (x, y), int(SCREEN_SIZE[1]/40))
            li = pos_list_black
            for i in panduan(x, y):
                if i >=4:Flag = True
            pos_list_black.append((x,y))
            Turn = not Turn
    if event.type == KEYDOWN and Turn and Flagger:
        if event.key == K_q:
            if (x,y) not in pos_li:
                pygame.draw.circle(background, (255, 255, 255), (x, y), int(SCREEN_SIZE[1]/40))
                li = pos_list_white
                for i in panduan(x, y):
                    if i >=4:Flag_w = True
                pos_list_white.append((x,y))
                Turn = not Turn
    pos_li = pos_list_white+pos_list_black
    pygame.display.update()
